<?php
namespace app\api\controller;
use app\api\controller\Pub;

use app\common\controller\Helpinfo;
use app\common\controller\Platurl;
use app\common\model\Agent;
use app\common\model\CardGeneral;
use app\common\model\CardJcList;
use app\common\model\CardJcLog;
use app\common\model\CardJcUserget;
use app\common\model\CardMember;
use app\common\model\CardQrcode;
use app\common\model\CardRank;
use app\common\model\CardUserBonus;
use app\common\model\CardUserFee;
use app\common\model\CardUserGet;
use app\common\model\Merchant;
use think\Request;
use think\Session;
use Wechat\Wxobjopen;

class CardThreeInfo extends Pub
{

    public function get_top_mach($param){
        $Merchant = new Merchant();
        $map['id'] = $param['merchantid'];
        $momach = $Merchant->where($map)->field('top_mid')->find();
        return $momach;
    }

    //商户卡券列表
    public function store_cards_list($param)
    {
        try {
//            $page=($param['page']-1)*10;
            $CardGeneral = new CardGeneral();
            $momach = $this->get_top_mach($param);
            //END
            $map['top_mid'] = $momach['top_mid'];
            if (!empty($param['is_cgive'])){
                $map['is_cgive'] = $param['is_cgive'];
            }
            $map['is_put'] = 1;
            $map['is_del'] = 1;     //未删除
            $list = $CardGeneral->where($map)
                ->field('id,card_id,title,get_limit,card_type')
//                ->limit($page, 10)
                ->select();
            $total = $CardGeneral->where($map)->count();
            if ($list) {
                $CardThreeMember = new CardThreeMember();
                foreach ($list as $k => $v) {
                    $list[$k]['card_type'] = $CardThreeMember->getcardtype($v['card_type']);
                }
                $data['return_code'] = "SUCCESS";
                $data['return_msg'] = "SUCCESS";
                $data['result_code'] = "SUCCESS";
                $data['result_msg'] = "SUCCESS";
                $data['total'] = $total;
                $data['info'] = $list;
            } else {
                $data = array("return_code" => "SUCCESS", "return_msg" => "SUCCESS", "result_code" => "FAIL", "result_msg" => "此会员无卡券",);
            }
        } catch (\Exception $e) {
            $data = array("return_code" => "FAIL", "return_msg" => "系统错误", "result_code" => "FAIL", "result_msg" => $e->getMessage());
        }
        return $data;
    }

    //商户次卡列表
    public function store_cardc_list($param)
    {
        try {
            $CardJcList = new CardJcList();
            $CardJcLog = new CardJcLog();
            $CardMember = new CardMember();
            $momach = $this->get_top_mach($param);
            //END
            $page=($param['page']-1)*10;
            $map['top_mid'] = $momach['top_mid'];
            $map['is_del'] = 1;
            $card = $CardMember->where($map)->field('logo_url,card_id')->find();
            $map['card_id'] = $card['card_id'];
            $list = $CardJcList->where($map)->limit($page, 10)->select();
            $total = $CardJcList->where($map)->count();
            if ($list) {
                $Platurl = new Platurl();
                $Plat_url = $Platurl->getagenturl('card_id',$card['card_id']);
                foreach ($list as $k=>$v){
                    $list[$k]['logo_url'] = $card['logo_url'];
                    $list[$k]['con_num'] = $CardJcLog->where(['cardjc_id'=>$v['id'],'type'=>2])->count('id');
                    $list[$k]['term_of_ch'] = '领取后'.$v['term_of'].'天有效';
                    $list[$k]['get_url'] = $Plat_url.'/mobile.php?s=/cardjc/cardjcinfo/card_id/'.$card['card_id'].'/id/'.$v['id'];
                }
                $data['return_code'] = "SUCCESS";
                $data['return_msg'] = "SUCCESS";
                $data['result_code'] = "SUCCESS";
                $data['result_msg'] = "SUCCESS";
                $data['total'] = $total;
                $data['info'] = $list;
            } else {
                $data = array("return_code" => "SUCCESS", "return_msg" => "SUCCESS", "result_code" => "FAIL", "result_msg" => "此会员无卡券",);
            }
        } catch (\Exception $e) {
            $data = array("return_code" => "FAIL", "return_msg" => "系统错误", "result_code" => "FAIL", "result_msg" => $e->getMessage());
        }
        return $data;
    }

    //商户计次卡购买列表
    public function store_cardc_buylist($param){
        try {
            $CardJcUserget = new CardJcUserget();
//            $momach = $this->get_top_mach($param);
            if (!empty($param['tims_id'])){
                $map['a.tims_id'] = $param['tims_id'];
            }
            //END
            $page=($param['page']-1)*10;
//            $map['top_mid'] = $momach['top_mid'];
            $map['a.mid'] = $param['merchantid'];
            $list = $CardJcUserget
                ->alias('a')
                ->join('wn_card_fans b','a.openid=b.openid','left')
                ->where($map)
                ->field('a.*,b.head_url,b.nickname')
                ->limit($page, 10)
                ->order('a.id desc')
                ->select();
            $total = $CardJcUserget->alias('a')->where($map)->count();
            if ($list) {
                $data['return_code'] = "SUCCESS";
                $data['return_msg'] = "SUCCESS";
                $data['result_code'] = "SUCCESS";
                $data['result_msg'] = "SUCCESS";
                $data['total'] = $total;
                $data['info'] = $list;
            } else {
                $data = array("return_code" => "SUCCESS", "return_msg" => "SUCCESS", "result_code" => "FAIL", "result_msg" => "此会员无卡券",);
            }
        } catch (\Exception $e) {
            $data = array("return_code" => "FAIL", "return_msg" => "系统错误", "result_code" => "FAIL", "result_msg" => $e->getMessage());
        }
        return $data;
    }


    //商户计次卡使用记录
    public function store_cardc_uselist($param){
        try {
            $CardJcLog = new CardJcLog();
//            $momach = $this->get_top_mach($param);
            if (!empty($param['tims_id'])){
                $map['a.cardjc_id'] = $param['tims_id'];
            }
            if (!empty($param['user_id'])){
                $map['a.user_id'] = $param['user_id'];
            }
            //END
            $page=($param['page']-1)*10;
//            $map['top_mid'] = $momach['top_mid'];
            $map['a.mid'] = $param['merchantid'];
            $map['a.type'] = 2;
            $list = $CardJcLog->alias('a')->join('wn_merchant b','a.mid=b.id','left')
                ->field('a.*,b.merchant')
                ->where($map)->limit($page, 10)->order('a.id desc')->select();
            $total = $CardJcLog->alias('a')->where($map)->count();
            if ($list) {
                foreach ($list as $k=>$v){
                    $list[$k]['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
                }
                $data['return_code'] = "SUCCESS";
                $data['return_msg'] = "SUCCESS";
                $data['result_code'] = "SUCCESS";
                $data['result_msg'] = "SUCCESS";
                $data['total'] = $total;
                $data['info'] = $list;
            } else {
                $data = array("return_code" => "SUCCESS", "return_msg" => "SUCCESS", "result_code" => "FAIL", "result_msg" => "此会员无卡券",);
            }
        } catch (\Exception $e) {
            $data = array("return_code" => "FAIL", "return_msg" => "系统错误", "result_code" => "FAIL", "result_msg" => $e->getMessage());
        }
        return $data;
    }


    //
    public function cardus_rank_list($param){
        try {
            $Merchant = new Merchant();
            $CardMember = new CardMember();
            $CardRank = new CardRank();
            $top_mid = $Merchant->where(['id'=>$param['merchantid']])->value('top_mid');
            $card = $CardMember->where(['top_mid'=>$top_mid])->find();
            $list = $CardRank->where(['card_id'=>$card['card_id']])->field('id,card_id,live_title,live,bonus,welfare')->select();
            $total = $CardRank->where(['card_id'=>$card['card_id']])->count();
            if ($list) {
                $res_data['return_code'] = "SUCCESS";
                $res_data['return_msg'] = "SUCCESS";
                $res_data['result_code'] = "SUCCESS";
                $res_data['result_msg'] = "SUCCESS";
                $res_data['total'] = $total;
                $res_data['info'] = $list;
            } else {
                $res_data['return_code'] = "SUCCESS";
                $res_data['return_msg'] = "SUCCESS";
                $res_data['result_code'] = "FAIL";
                $res_data['result_msg'] = "未设置等级";
            }
        } catch (\Exception $e) {
            $res_data['return_code'] = "FAIL";
            $res_data['return_msg'] = "系统错误";
            $res_data['result_code'] = "FAIL";
            $res_data['result_msg'] = $e->getMessage();
        }
        return $res_data;
    }

    //is_pay  is_pay_balance  price  pay_profiles
    public function cardus_pay_set($param){
        try {
            $Merchant = new Merchant();
            $mach = $Merchant->where(['id'=>$param['merchantid']])->field('top_mid')->find();
            $CardMember = new CardMember();
            $card = $CardMember->where(['top_mid'=>$mach['top_mid']])->field('top_mid,card_id')->find();
            //END
            $data['is_pay'] = $param['is_pay'];
            $data['is_pay_balance'] = $param['is_pay_balance'];
            $data['price'] = $param['price'];
            $data['pay_profiles'] = $param['pay_profiles'];
            $wxres = $this->paycarduswx($card['card_id'], $param,$card);
            if ($wxres['errcode'] == 0) {
                $map['card_id'] = $card['card_id'];
                $res = $CardMember->allowField(true)->save($data,$map);
                if ($res) {
                    $res_data['return_code'] = "SUCCESS";
                    $res_data['return_msg'] = "SUCCESS";
                    $res_data['result_code'] = "SUCCESS";
                    $res_data['result_msg'] = "设置完成";
                } else {
                    $res_data['return_code'] = "SUCCESS";
                    $res_data['return_msg'] = "SUCCESS";
                    $res_data['result_code'] = "FAIL";
                    $res_data['result_msg'] = "设置失败";
                }
            } else {
                //错误提示翻译
                $Helpinfo = new Helpinfo();
                $errmsg = $Helpinfo->gethelpinfo(0,$wxres['errcode'],$wxres['errmsg']);
                //END
                $res_data['return_code'] = "SUCCESS";
                $res_data['return_msg'] = "SUCCESS";
                $res_data['result_code'] = "FAIL";
                $res_data['result_msg'] = "设置失败:".$errmsg;
            }
        } catch (\Exception $e) {
            $res_data['return_code'] = "FAIL";
            $res_data['return_msg'] = "系统错误";
            $res_data['result_code'] = "FAIL";
            $res_data['result_msg'] = $e->getMessage();
        }
        return $res_data;
    }

    //
    public function cardus_pay_info($param){
        try {
            $Merchant = new Merchant();
            $mach = $Merchant->where(['id'=>$param['merchantid']])->field('top_mid')->find();
            $CardMember = new CardMember();
            $card = $CardMember->where(['top_mid'=>$mach['top_mid']])->field('top_mid,card_id,is_pay,is_pay_balance,price,pay_profiles')->find();
            if ($card) {
                $datas['top_mid'] = $card['top_mid'];
                $datas['card_id'] = $card['card_id'];
                $datas['is_pay'] = $card['is_pay'];
                $datas['is_pay_balance'] = $card['is_pay_balance'];
                $datas['price'] = $card['price'];
                $datas['pay_profiles'] = $card['pay_profiles'];
                //END
                $res_data['return_code'] = "SUCCESS";
                $res_data['return_msg'] = "SUCCESS";
                $res_data['result_code'] = "SUCCESS";
                $res_data['result_msg'] = "SUCCESS";
                $res_data['info'] = $datas;
            } else {
                $res_data['return_code'] = "SUCCESS";
                $res_data['return_msg'] = "SUCCESS";
                $res_data['result_code'] = "FAIL";
                $res_data['result_msg'] = "设置失败";
            }
        } catch (\Exception $e) {
            $res_data['return_code'] = "FAIL";
            $res_data['return_msg'] = "系统错误";
            $res_data['result_code'] = "FAIL";
            $res_data['result_msg'] = $e->getMessage();
        }
        return $res_data;
    }

    //开启付费 微信接口
    private function paycarduswx($card_id='', $arr,$card)
    {
        $Platurl = new Platurl();
        $url['platurl'] = $Platurl->getagenturl('top_mid',$card['top_mid']);
        $data['card_id'] = $card_id;
        //是否开启付费
        if ($arr['is_pay'] == 1) {
            $data['member_card']['wx_activate_after_submit'] = true;
            $data['member_card']['wx_activate_after_submit_url'] = $url['platurl'] . '/mobile.php?s=/cardshop/activcardus';
        } else {
            $data['member_card']['wx_activate_after_submit'] = true;
            $data['member_card']['wx_activate_after_submit_url'] = $url['platurl'] . '/mobile.php?s=/cardacitvate/cardfire/';
        }
        $options = $this->options($card_id);
        $wxObj = new Wxobjopen($options);
        //向接口发送修改
        $res = $wxObj->updateUserCard($data);
        return $res;
    }

    public function cardqinfotest($params)
    {
        try {
            //   return json($params);
            $Merchant = new Merchant();
            $top_mid = $Merchant->where('id', $params['merchantid'])->value('top_mid');
            $midlist = $Merchant->where('top_mid', $top_mid)->field('id')->select();
            foreach ($midlist as $k => $v) {
                $midarr[] = $v['id'];
            }

            $mid = implode(',', $midarr);
            $map['mid'] = ['in', $mid];
            $map['top_mid'] = $top_mid;
            $map['code'] = $params['code'];
            $map['card_type'] = ['neq', 'MEMBER_CARD'];
            $CardUserGet = new CardUserGet();
            $CardGeneral = new CardGeneral();
            //用户
            $userinfo = $CardUserGet->where($map)->field('card_id,code,create_time,have,statu')->find();

            if ($userinfo) {

                if ($userinfo['statu'] == 0) {
                    if ($userinfo['have'] == 1) {
                        $mab['card_id'] = $userinfo['card_id'];
                        $cardinfo = $CardGeneral->where($mab)->field('title,brand_name,logo_url,card_type')->find();
                        $brand_name = $Merchant->where(['id' => $params['merchantid']])->value('merchant');
                        //卡券详情
                        $userinfos['card_id']=$userinfo['card_id'];
                        $userinfos['code']=$userinfo['code'];
                        $userinfos['create_time']=$userinfo['create_time'];
                        $userinfos['have']=$userinfo['have'];
                        $userinfos['statu']=$userinfo['statu'];
                        $userinfos['title'] = $cardinfo['title'];
                        $userinfos['merchantname'] = $brand_name;
                        $userinfos['logo_url'] = $cardinfo['logo_url'];
                        $userinfos['card_type'] = $cardinfo['card_type'];
                        //返回核销状态    0可用/1不可用
                        $userinfos['consume_statu'] = $this->consumestatu($userinfo['card_id'], $userinfo['code']);
                        $rarr['return_code'] = "SUCCESS";
                        $rarr['return_msg'] = 'ok';
                        $rarr['result_code'] = 'SUCCESS';
                        $rarr['result_msg'] = "获取成功";

                        $rarr['info'] = $userinfos;
                    } elseif ($userinfo['have'] == 0) {
                        $rarr['return_code'] = "SUCCESS";
                        $rarr['return_msg'] = 'ok';
                        $rarr['result_code'] = 'FAIL';
                        $rarr['result_msg'] = "卡券状态异常";
                    }
                } elseif ($userinfo['statu'] == 2) {
                    $rarr['return_code'] = "SUCCESS";
                    $rarr['return_msg'] = 'ok';
                    $rarr['result_code'] = 'FAIL';
                    $rarr['result_msg'] = "该卡券已核销";
                }

            } else {
                $rarr['return_code'] = "SUCCESS";
                $rarr['return_msg'] = 'ok';
                $rarr['result_code'] = 'FAIL';
                $rarr['result_msg'] = "卡券编号不存在";
            }
        } catch (\Exception $e) {
            $rarr['return_code'] = "FAIL";
            $rarr['return_msg'] = '系统错误';
            $rarr['result_code'] = 'FAIL';
            $rarr['result_msg'] = '系统错误' . $e->getMessage();
        }
        return $rarr;
    }

}
